// Copyright 2010 Alexandre Tristan St-Cyr <tristan.stcyr@gmail.com>

package com.codeplex.loadjava;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import junit.framework.Assert;

import org.junit.Test;

import com.codeplex.loadjava.JavaLoaderCli;

/**
 * Some system level test runs. 
 * These tests do not actually fail when an error occurs but are helpful for trying out the loader.
 */
public class JavaClassLoaderTest {
	
	@Test
	public void SimpleClassLoadingTest()
	{
		loadFromPath("inputbin"); 
	}
	
	@Test
	public void largeClassLoadingTest()
	{
		loadFromPath("lib\\mockito-all-1.8.5.jar");
	}
	
	private void loadFromPath(String path)
	{
		Properties props = loadProperties();
		long timeInMillis = System.currentTimeMillis();
		JavaLoaderCli.main(new String[] { path, 
				"-targetFolder", "test",
				"-server", props.getProperty("server"), 
				"-database", props.getProperty("database"), 
				"-username", props.getProperty("username"), 
				"-password", props.getProperty("password") });
		System.out.println((System.currentTimeMillis()-timeInMillis)/1000.0);
	}
	
	private static Properties loadProperties()
	{
		File file = new File("config/database.properties");
		if (!file.exists()) {
			System.out.println("Could not find database config file "+file.getPath()+
					". Did you forget to create your file from the template?");
			Assert.fail();
		}
		Properties props = null;
		FileInputStream propertiesStream = null;
		try
		{
			try
			{
				propertiesStream = new FileInputStream(file);
				props = new Properties();
				props.load(propertiesStream);
			} finally {
				if (propertiesStream != null)
					propertiesStream.close();
			}
		} catch(IOException e) {
			e.printStackTrace();
			Assert.fail(e.getMessage());
		}
		return props;
	}
}
